package com.isoftstone.demo.web;

import com.isoftstone.demo.pojo.Agency;
import com.isoftstone.demo.pojo.Depot;
import com.isoftstone.demo.pojo.User;
import com.isoftstone.demo.service.AgencyService;
import com.isoftstone.demo.service.DepotService;
import com.isoftstone.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class UserController {
	
	@Autowired
	UserService us;
	@Autowired
	DepotService depotService;
	@Autowired
	AgencyService agencyService;
	
	@RequestMapping("/login")
	public String login(Integer type, Model model) {
		model.addAttribute("message","OCP运营管理后台系统");
		if (type != null ) {
			if (type.equals(1)) {
				model.addAttribute("err",  "账号不存在或密码错误");
			} else {
				model.addAttribute("err",  "请先登陆");
			}
		}
		return "login";
	}

	@RequestMapping("/show")
	public String show(User u,HttpSession session,Model model) throws Exception {
		User user = us.login(u.getUsername(), u.getPassword());
		if(user!=null&&u.getUsername().equals(user.getUsername())&&u.getPassword().equals(user.getPassword())) {
			session.setAttribute("user", user);
			if(user.getRoleType()==1){
                List<Depot> list = depotService.findAllDepot();
			    Agency agency = agencyService.findAgencyByName(user.getUsername());
				model.addAttribute("depotlist",list);
			    model.addAttribute("agency",agency);
			    return "submitorder";
            }else {
                return "index";
            }
		}
		return "redirect:/login?type=1";

	}
}
